package com.navinfo.data.esper.cep.linkid;

import com.espertech.esper.client.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.*;

public class SearchLinkidMain {
    private static final Logger log = LoggerFactory.getLogger(SearchLinkidMain.class);

    public static void main(String[] args) throws IOException, InterruptedException {
        // Configure engine with event names to make the statements more readable.
        // This could also be done in a configuration file.
        Configuration configuration = new Configuration();
        configuration.addEventType("TerminalLink", TerminalLink.class.getName());

        // Get engine instance
        EPServiceProvider epService = EPServiceProviderManager.getProvider("linkid", configuration);

        //创建测试数据集
        Map<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>();
        for (int i = 9001; i < 9011; i++) {
            List<Integer> li = new ArrayList<Integer>();
            for (int j = 1; j < 11; j++) {
                li.add(j+9010-i);
            }
            map.put(i,li);
        }

        List<Integer> li = new ArrayList<Integer>();
        for (int j = 1; j < 11; j++) {
            li.add(3);
        }
        map.put(9011,li);

        List<Integer> roadLinkids=new ArrayList<Integer>();
        for (int j = 1; j < 4; j++) {
            roadLinkids.add(j);
        }

        List<Integer> tidList=new ArrayList<Integer>();

        for (int i = 9001; i <= 9011; i++) {
            //Clear engine statement
            epService.getEPAdministrator().destroyAllStatements();
            epService.removeAllStatementStateListeners();
            //Set up statements
//            String pattern = "(([:1] (a0=TerminalLink(linkid=1)) until (b0=TerminalLink(linkid=1))) -> ([:1] (a=TerminalLink(linkid=1)) until (b=TerminalLink(linkid=2))) -> ([:1] (c=TerminalLink(linkid=1)) until (d=TerminalLink(linkid=3)))) and TerminalLink(tid="+i+")";
            StringBuffer pattern = new StringBuffer().append("(");

            int roadlinkid = 0;
            if (roadLinkids.size()>0) {
                for(int z=0;z<roadLinkids.size();z++){
                    roadlinkid = roadLinkids.get(z);
                    if (z==(roadLinkids.size()-1)) {
                        pattern.append("([:1] (TerminalLink(linkid=").append(roadlinkid).append(")) until (b=TerminalLink(linkid=").append(roadlinkid).append(")))) and TerminalLink(tid=").append(i).append(")");
                    } else {
                        pattern.append("([:1] (TerminalLink(linkid=").append(roadlinkid).append(")) until (TerminalLink(linkid=").append(roadlinkid).append("))) -> ");
                    }

                }
                EPStatement statement = epService.getEPAdministrator().createPattern(pattern.toString());
                LinkidPatternListener linkidPatternListener = new LinkidPatternListener();
                statement.addListener(linkidPatternListener);

                EPRuntime runtime = epService.getEPRuntime();

                for (int j = 0; j < map.get(i).size(); j++) {
                    TerminalLink tl = new TerminalLink();
                    tl.setTid(i);
                    tl.setLinkid(map.get(i).get(j));
                    runtime.sendEvent(tl);
                }
                if (linkidPatternListener.getLastEvent()!=null) {
                    tidList.add(((TerminalLink) linkidPatternListener.getLastEvent().get("b")).getTid());
                    log.debug("--------------------b query returned: " + ((TerminalLink) linkidPatternListener.getLastEvent().get("b")).getTid());
                }

            }

        }

        Iterator<Integer> it2 = tidList.iterator();
        while (it2.hasNext()) {
            log.info(it2.next() + " ");
        }

        epService.destroy();
    }
}
